package nicewarm.com.airbansearchanimation.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import nicewarm.com.airbansearchanimation.R;
import nicewarm.com.airbansearchanimation.glide.GlideRoundTransform;
import nicewarm.com.airbansearchanimation.model.HuaweiFirstPageMultipleItem;
import nicewarm.com.airbansearchanimation.view.BannerHolderView;
import nicewarm.com.airbansearchanimation.view.SpacesItemDecoration;

public class HuaweiFirstpageRecylerAdapter extends BaseMultiItemQuickAdapter<HuaweiFirstPageMultipleItem, BaseViewHolder> {
    private final String Tag = HuaweiFirstpageRecylerAdapter.class.getSimpleName();
    private Context mContext;
    private SpacesItemDecoration mSpacesItemDecoration;

    public HuaweiFirstpageRecylerAdapter(Context context, List data) {
        super(data);
        mContext = context;
        addItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_BANNER, R.layout.item_banner);
        addItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_GALLERY, R.layout.item_gallery);
        addItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_TWO_IMAGE, R.layout.item_two_image);
        mSpacesItemDecoration = new SpacesItemDecoration(mContext.getResources().getDimensionPixelSize(R.dimen.list_item_default_padding));
    }

    @Override
    protected void convert(BaseViewHolder helper, HuaweiFirstPageMultipleItem item) {
        switch (helper.getItemViewType()) {
            case HuaweiFirstPageMultipleItem.ITEM_LAYOUT_GALLERY: {
                helper.setText(R.id.app_category, item.getAppCategory());

                RecyclerView applist = helper.getView(R.id.apps_rv);
                LinearLayoutManager applistLayoutManager = new LinearLayoutManager(mContext);
                applistLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                applist.setLayoutManager(applistLayoutManager);
                applist.removeItemDecoration(mSpacesItemDecoration); // 先删除　不然会一直叠加
                applist.addItemDecoration(mSpacesItemDecoration);
                AppListAdapter listAdapter = new AppListAdapter(item.getApps());
                applist.setAdapter(listAdapter);
                break;
            }
            case HuaweiFirstPageMultipleItem.ITEM_LAYOUT_TWO_IMAGE: {
                if (item.getImages() != null && item.getImages().size() >= 2) {
                    loadImageRound(item.getImages().get(0), helper.getView(R.id.first_iv));
                    loadImageRound(item.getImages().get(1), helper.getView(R.id.second_iv));
                }
                break;
            }

            case HuaweiFirstPageMultipleItem.ITEM_LAYOUT_BANNER: {
                ConvenientBanner convenientBanner = helper.getView(R.id.convenientBanner);
                if (convenientBanner != null) {
                    convenientBanner.setPages(new CBViewHolderCreator<BannerHolderView>() {
                        @Override
                        public BannerHolderView createHolder() {
                            return new BannerHolderView();
                        }
                    }, item.getImages())
                            .setPointViewVisible(true)
                            .startTurning(6000);
                } else {
                    Log.e(Tag, "convenientBanner is null");
                }
            }
        }
    }

    private void loadImageRound(String url, View view) {
        if (TextUtils.isEmpty(url)) {
            Log.e(Tag, "iamge url is null");
        } else {
            Glide.with(mContext)
                    .load(url)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .transform(new GlideRoundTransform(mContext, 8))
                    .crossFade(500)
                    .into((ImageView) view);
        }
    }

    private void loadImage(String url, View view) {
        if (TextUtils.isEmpty(url)) {
            Log.e(Tag, "iamge url is null");
        } else {
            Glide.with(mContext)
                    .load(url)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .crossFade(500)
                    .into((ImageView) view);
        }
    }

}
